import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../event/DataCallBack.dart';
import '../../nets/NetWorkManager.dart';
import '../../services/AudioService.dart';
import '../../utils/tips.dart';

class UseInfos  {
    String id ='0';
    String name = '';
    String avatar ='';
    String email ='';
}

class MineViewController extends GetxController {
  var infos = <UseInfos>{}.obs;
  var realname = 'test1'.obs;
  var email = ''.obs;
  var avatar = ''.obs;
  var id = '0'.obs;
  var phone = ''.obs;
  var localAvatar =''.obs;

  var appVersion = ''.obs;

  AudioService audioService = Get.find<AudioService>();

  @override
  void onInit() {
    super.onInit();
    Future.delayed(Duration(milliseconds: 600),(){
      getInfos();
    });
    getAppVersion();
 }

  void getAppVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    String version = packageInfo.version;
    appVersion.value = version;
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

//   获取个人信息
  void getInfos() async {
    var curTimeStamp = DateTime
        .now()
        .millisecondsSinceEpoch;
    NetWorkManager.getInstance().requestBussiness(
        NetWorkManager.Get_USER_INFOS, {
      '_t': curTimeStamp,
    }, NetWorkManager.methodGET, DataCallBack(
        businessCallBack: (v) {
          var usern = v['username'];
          var realn = v['realname'];
          realname.value = (realn == null) ? usern : realn;
          email.value = (v['email'] == null) ? '' : v['email'];
          avatar.value = (v['avatar'] == null) ? '' : v['avatar'];
          id.value = v['id'];
          phone.value = v['phone'];
        },
        errorCallBack: (v) {
          Tips.showToast(v);
        }

    ));
  }

//   更新头像
  void updateAvatar(avataUrl) {
    avatar.value = avataUrl;
  }

// 更新昵称
  void updateName(name) {
    realname.value = name;
  }

//  更新本地头像（上传后）
void updateLocalAavatar(path){
    localAvatar.value = path;
}

}
